﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using Domain;
using Persistence.Dao;
using Persistence;
using IBatisNet.Common;
namespace web
{
    public partial class cartStep1 : memberBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
        protected List<OrderDetailMen> menlist = new List<OrderDetailMen>();
        protected List<OrderDetailKou> koulist = new List<OrderDetailKou>();
        protected List<OrderDetailOther> otherlist = new List<OrderDetailOther>();

        private void GetStoreDetail()
        {
            if (Session["OrderDetailMen"] != null)
                menlist = (List<OrderDetailMen>)Session["OrderDetailMen"];
            if (Session["OrderDetailKou"] != null)
                koulist = (List<OrderDetailKou>)Session["OrderDetailKou"];
            if (Session["OrderDetailOther"] != null)
                otherlist = (List<OrderDetailOther>)Session["OrderDetailOther"];
        }
        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            Order entity_Order = new Order();

            entity_Order.userId = ((Domain.User)Session["user"]).userId;
            entity_Order.payType = txt_Order_payType.Value.Trim();
            entity_Order.receiverIsSameToUser = cbIsSameToRegister.Checked;
            if (entity_Order.receiverIsSameToUser)
            {
                entity_Order.receiverName = base.u.trueName;
                entity_Order.receiverSex = base.u.sex;
                entity_Order.receiverAddress = base.u.addressCorp;
                entity_Order.receiverPostCode = base.u.postCode == 0 ? "" : base.u.postCode.ToString();
                entity_Order.receiverTelephone = base.u.telephone;
                entity_Order.receiverMobile = base.u.mobile;
                entity_Order.receiverEmail = base.u.email;
            }
            else
            {
                entity_Order.receiverName = txt_Order_receiverName.Value.Trim();
                entity_Order.receiverSex = Utils.ObjectToInt32(txt_Order_receiverSex.Value.Trim(), 0);
                entity_Order.receiverAddress = txt_Order_receiverAddress.Value.Trim();
                entity_Order.receiverPostCode = txt_Order_receiverPostCode.Value.Trim();
                entity_Order.receiverTelephone = txt_Order_receiverTelephone.Value.Trim();
                entity_Order.receiverMobile = txt_Order_receiverMobile.Value.Trim();
                entity_Order.receiverEmail = txt_Order_receiverEmail.Value.Trim();
            }
            entity_Order.orderRemark = txt_Order_orderRemark.Text.Trim();
            entity_Order.orderTime = DateTime.Now;
            #region 生成订单Code
            DateTime yearBegin = new DateTime(DateTime.Now.Year, 1, 1);
            DateTime yearEnd = DateTime.Now;
            int count = Singleton<OrderDao>.Instance.GetCount("userid=" + base.u.userId + " and orderTime between '" + yearBegin.ToString("yyyy-MM-dd HH:mm:ss") + "' and '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            entity_Order.orderCode = base.u.userCode + DateTime.Now.ToString("yy") + (count+1).ToString().PadLeft(4, '0');
            #endregion
            entity_Order.deleteFlag = 0;
            bool bResult = false;
    
            using (IDalSession session = BaseMap.Mapper.BeginTransaction())
            {
                int orderid = Singleton<OrderDao>.Instance.Insert(entity_Order);
                GetStoreDetail();
                for (int i = 0; i < menlist.Count; i++)
                {
                    OrderDetailMen tmp = menlist[i];
                    tmp.orderId = orderid;
                    Singleton<OrderDetailMenDao>.Instance.Insert(tmp);
                }
                for (int i = 0; i < koulist.Count; i++)
                {
                    OrderDetailKou tmp = koulist[i];
                    tmp.orderId = orderid;
                    Singleton<OrderDetailKouDao>.Instance.Insert(tmp);
                }
                for (int i = 0; i < otherlist.Count; i++)
                {
                    OrderDetailOther tmp = otherlist[i];
                    tmp.orderId = orderid;
                    Singleton<OrderDetailOtherDao>.Instance.Insert(tmp);
                }
                bResult = true;
                session.Complete(); // Commit
            }

            if (bResult)
            {
                Message.alertAndClose("操作成功");
                Session["OrderDetailMen"] = null;
                Session["OrderDetailKou"] = null;
                Session["OrderDetailOther"] = null;
            }
            else
            {
                Message.alert("操作失败，请检查您的数据");
            }
        }
    }
}
